/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.*;
import FrameWork.Audio.RSAudio;


import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;


/**
*
 * @author ThanhTri
 */
public class State_Win extends IState{ 
    int m_OldKey = 0;
    int m_CurLevel;
    private State_MainGame m_MainGame;
    private Sprite m_Dialog;
    private Font_Large m_Font;
    private int m_Chose;
    private Sprite m_Ball;
    Rectangle[] m_RectButton = new Rectangle[2];
    int m_Check;
    
     public State_Win (IPlay _Play,State_MainGame _MainGame){
        super(_Play);   
        m_MainGame = _MainGame;
        m_IDState = Global.STATE_WIN;
        try {
            m_Dialog = new Sprite (Image.createImage("/Image/dialog.png"),
                                204, 235);
            m_Ball = new Sprite (Image.createImage("/Image/ball.png"),
                            60, 60);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        m_Font = (Font_Large) RSFont.getRS().getLarge();
        m_Chose = 0;
        
        for(int i =0; i<2; i++)
            m_RectButton[i] = new Rectangle(32 + i*114, 186, 60, 60);
        m_Check = 0;
        m_CurLevel = Level.get().getCurLevel();
    }  
    public void PointerPressed(int x, int y){
        for(int i =0; i<2; i++){
            if(m_RectButton[i].Intersects(new Point(x,y))){
                m_Chose = i+1;
                RSAudio.getRS().PlayExplosion();
                if(m_Chose==1 && m_Check == 0){
                     Level.get().UnlockNextLevel();
                     m_Check += 1;
                     m_MainGame.getPlay().setNextState(
                     new State_Menu(m_Play));
                }
                if(Level.get().getCurLevel()< Level.get().getTotalLevel()){
                    if(m_Chose==2 && m_Check == 0){
                     m_Check += 1;
                     if(Level.get().NextLevel())
                         m_MainGame.getPlay().setNextState(
                         new State_Loading(m_Play));
                    }
                }
                
            }
        }
    }
    public void KeyPressed (int _Keycode){
        
        switch(_Keycode)
        {
            // <editor-fold defaultstate="collapsed"  desc="Key : LEFT-RIGHT-UP-DOWN">
            case GameCanvas.RIGHT_PRESSED:
                if (m_OldKey == 0){
                    RSAudio.getRS().PlayBubble();
                    m_Chose = 2;
                }
                break;
            case GameCanvas.LEFT_PRESSED:
                if (m_OldKey == 0){
                    RSAudio.getRS().PlayBubble();
                    m_Chose = 1;
                }
                break;     
            // </editor-fold>  
             case GameCanvas.FIRE_PRESSED:
                 if(m_Chose!=0){
                     RSAudio.getRS().PlayExplosion();
                 }
                 if(m_Chose==1 && m_Check == 0){
                     m_Check += 1;
                     Level.get().UnlockNextLevel();    
                     m_MainGame.getPlay().setNextState(
                          new State_Menu(m_Play));
                     
                 }
                 if(m_Chose==2 && m_Check == 0){
                     m_Check += 1;
                     if(Level.get().NextLevel())
                         m_MainGame.getPlay().setNextState(
                          new State_Loading(m_Play));
                 }
                break;     
        }
        m_OldKey = _Keycode;
    }
    public void Init (){
        
    }
    public void Update (float _Time){
    }
    
    
    public void Render (){
        m_Dialog.setPosition(18, 20);
        m_Dialog.paint(m_Graphics);
        m_Font.DrawString("-- You Win --", 60, 140, m_Graphics);
        
        m_Font.DrawString("Well done !!! lv:" + m_CurLevel, 40, 160, m_Graphics);
        
        if(m_Chose==1){
            m_Ball.setFrame(1);
        }else{
            m_Ball.setFrame(0);
        }
        
        m_Ball.setPosition(32, 186);
        m_Ball.paint(m_Graphics);
        m_Font.DrawString("Menu", 42, 206, m_Graphics);
        
        if(Level.get().getCurLevel()< Level.get().getTotalLevel()){
               if(m_Chose==2){
                m_Ball.setFrame(1);
            }else{
                m_Ball.setFrame(0);
            }

            m_Ball.setPosition(146, 186);
            m_Ball.paint(m_Graphics);
            m_Font.DrawString("Next", 154, 206, m_Graphics); 
        }
    }
    public void Destroy (){       
        m_Dialog = null;
        m_Ball = null;    
    }
    
}